package edu.xust.MetadataGenerator.domain;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.google.gson.annotations.Expose;

public class APIValidator {
	private static final long id = Table.generateId();

	public static final Map<String, String> BUILTIN_MESSAGE = new HashMap<String, String>();
	
	static{
		BUILTIN_MESSAGE.put("required", "对象不能为空！");
		BUILTIN_MESSAGE.put("requiedstring", "对象字符串不能为空！");
		BUILTIN_MESSAGE.put("stringlength", "字符串长度应在{minLength}~{maxLength}之间！");
		BUILTIN_MESSAGE.put("int", "对象应为整数！");
		BUILTIN_MESSAGE.put("double", "对象应为浮点数！");
		BUILTIN_MESSAGE.put("date", "对象应为日期格式！");
		BUILTIN_MESSAGE.put("email", "对象应为EMAIL格式！");
		BUILTIN_MESSAGE.put("url", "对象应为URL地址！");
		BUILTIN_MESSAGE.put("fieldexpression", "对象应满足：");
		BUILTIN_MESSAGE.put("expression", "对象应满足：");
		BUILTIN_MESSAGE.put("visitor", "对象应满足：");
		BUILTIN_MESSAGE.put("regex", "对象应满足：");
	}
	
	@Expose
	private String type;

	@Expose
	private String message;
	@Expose
	private String messageU;
	
	@Expose
	private List<APIValidatorParam> params;

	public APIValidator(){
		type = "required";
		params = new LinkedList<APIValidatorParam>();
	}
	
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type.replace(" ", "");
		String m = (String)BUILTIN_MESSAGE.get(type.toLowerCase());
		if(null != m && (null == message || message.length() == 0)){
			message = m;
			messageU = _getMessageU();
		}
	}
	public List<APIValidatorParam> getParams() {
		return params;
	}
	public void setParams(List<APIValidatorParam> params) {
		this.params = params;
	}
	public static long getId() {
		return id;
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return type;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
		messageU = _getMessageU();
	}
	public String _getMessageU() {
		String result = "";
		if(null != this.message){
			char[] bs = this.message.toCharArray();
			if(null != bs){
				for(char b : bs){
					result += FieldValidator.generateUnicode(b);
				}
			}
		}
		System.out.println("U:" + result);
		return result;
	}
	public String getMessageU() {
		return _getMessageU();
	}
	public void setMessageU(String m){
		
	}

}
